package cn.yht.wang.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtils {

    private  static  final  String SING = "!QAZ1qaz";
    public  static  String getToken(Map<String,String> map)
    {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,7);
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        return builder.withExpiresAt(instance.getTime())
                        .sign(Algorithm.HMAC256(SING));
        //System.out.println(token);
    }

    public  static  DecodedJWT verify(String token)
    {
//      JWTVerifier require = JWT.require(Algorithm.HMAC256(SING)).build();
//      DecodedJWT verify = require.verify(token);
        return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

    //获取token中的信息
    public  static  DecodedJWT getTokenInfo(String token)
    {
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
        return verify;
    }
}
